package com.team.appinfo.controller;

import com.team.appinfo.entity.AppVersion;
import com.team.appinfo.service.AppVersionService;
import com.team.appinfo.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class AppVersionController {

    @Autowired
    private AppVersionService appVersionService;


    @RequestMapping("/goAppVersion")
    public String addAppVersion(Long id, Model model){
        List<AppVersion> list = appVersionService.getAppVersionById(id);
        model.addAttribute("appVersions",list);
        model.addAttribute("appid",id);
        return "developer/appversionadd";
    }



    @RequestMapping("addAppVersion")
    public String addAppVersion(@RequestParam(value = "a_downloadLink",required = false) CommonsMultipartFile file,
                                HttpServletRequest request, AppVersion appVersion){

        try {
            String path = "upload/logo";
            String realPath = request.getServletContext().getRealPath(path);
            String fileName = FileUploadUtil.uploadFile(realPath, file);
            appVersion.setApkfilename(file.getOriginalFilename());
            appVersion.setDownloadlink(path+"/"+fileName);
            appVersionService.insertAppVersion(appVersion);
            return "redirect:goAppVersion?id="+appVersion.getAppid();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:jsp/error.jsp";
    }
}
